# 发布文章页面对象
from selenium.webdriver.common.by import By

from base.ttmp.base_page import BasePage, BaseHandle
from utils import UtilsDriver, choice_channel


class PublishArticlePage(BasePage):
    def __init__(self):
        super().__init__()
        # 文章标题
        self.title = By.CSS_SELECTOR, "[placeholder='文章名称']"
        # 文章内容（因为文章内容输入框为副文本框，所以需要定位到Body标签元素即可）
        self.content = By.CSS_SELECTOR, "#tinymce"
        # frame元素对象
        self.content_frame = By.CSS_SELECTOR, "#publishTinymce_ifr"
        # 封装选择
        self.cover = By.XPATH, "//*[@role='radiogroup']/label[4]/span"
        # 频道选择
        self.channel = By.CSS_SELECTOR, "[placeholder='请选择']"
        # 发布按钮
        self.publish_btn = By.XPATH, "//*[@class='el-button filter-item el-button--primary']"

        # 提示信息框
        self.msg = By.CSS_SELECTOR, ".el-message__content"
    # 定位文章标题
    def find_title(self):
        return self.get_element(self.title)

    # 定位iframe元素对象
    def find_content_frame(self):
        return self.get_element(self.content_frame)

    # 定位文章内容输入框
    def find_content(self):
        return self.get_element(self.content)

    # 定位封面
    def find_cover(self):
        return self.get_element(self.cover)

    # 定位频道选择框
    def find_channel(self):
        return self.get_element(self.channel)

    # 定位发布按钮
    def find_publish_btn(self):
        return self.get_element(self.publish_btn)

    # 定位提示信息框
    def find_msg(self):
        return self.get_element(self.msg)

class PublishArticleHandle(BaseHandle):
    def __init__(self):
        self.publish_article_page =PublishArticlePage()
        self.driver = UtilsDriver.get_ttmp_driver()  # 因为后面需要用到driver，所以会获取自媒体驱动

    # 输入文章标题
    def input_title(self, title):
        self.input_text(self.publish_article_page.find_title(), title)

    # 输入文章内容
    def input_content(self, content):
        # 切换到iframe
        self.driver.switch_to.frame(self.publish_article_page.find_content_frame())
        # 输入文章的内容
        self.input_text(self.publish_article_page.find_content(), content)
        # 切回到默认首页
        self.driver.switch_to.default_content()

    # 选择封面
    def select_cover(self):
        self.publish_article_page.find_cover().click()

    # 选择频道
    def select_channel(self, channel):
        choice_channel(self.driver, self.publish_article_page.find_channel(), channel)

    # 点击发布按钮
    def click_publish_btn(self):
        self.publish_article_page.find_publish_btn().click()

    # 获取提示信息
    def get_msg(self):
        return self.publish_article_page.find_msg().text


class PublishArticleProxy:
    def __init__(self):
        self.publish_article_handle = PublishArticleHandle()

    # 发布文章
    def publish_article(self, title, content, channel):
        # 输入文章标题
        self.publish_article_handle.input_title(title)
        # 输入文章内容
        self.publish_article_handle.input_content(content)
        # 选择封面
        self.publish_article_handle.select_cover()
        # 选择频道
        self.publish_article_handle.select_channel(channel)
        # 点击发布按钮
        self.publish_article_handle.click_publish_btn()

    # 获取发布成功的提示信息
    def get_success_msg(self):
        return self.publish_article_handle.get_msg()
